package cn.edu.njuit.lesson9heros.service.impl;

import cn.edu.njuit.lesson9heros.myioc.MyComponent;
import cn.edu.njuit.lesson9heros.pojo.Equipment;
import cn.edu.njuit.lesson9heros.pojo.Hero;
import cn.edu.njuit.lesson9heros.pojo.HeroEquipment;
import cn.edu.njuit.lesson9heros.service.EquipmentService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONReader;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;

@MyComponent(name="equipmentService")
@Service
public class EquipmentServiceImpl implements EquipmentService {
    /**
     * 装备库:装备id为key，装备实体为值
     */
    private Map<Integer,Equipment> equipmentMap=new HashMap<>();

    /**
     * 英雄推荐出装方案库，json读取
     */
    private Map<Integer,List<Integer[]>> heroEquipmentMap=new HashMap<>();

    /**
     * 数据初始化
     */
    private void initEquipmentData(){
        String [] types={"","攻击","法术","防御","移动","打野","","游走"};
        String classPath=this.getClass().getClassLoader().getResource("").getPath();
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(classPath+"item.json");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        JSONReader jsonReader = new JSONReader(fileReader);
        String heroString=jsonReader.readString();
        //读取json文件，转为JSONArray
        JSONArray jsonArray = JSON.parseArray(heroString);
        if(jsonArray.size()>0){
            for(int i=0;i<jsonArray.size();i++){
                JSONObject equipmentJson = jsonArray.getJSONObject(i);
                Equipment e=new Equipment();
                Integer id=equipmentJson.getInteger("item_id");
                e.setEquipmentId(id);
                e.setEquipmentName(equipmentJson.getString("item_name"));
                e.setEquipmentType(types[equipmentJson.getInteger("item_type")]);
                e.setDescription(equipmentJson.getString("des1"));
                e.setBuyPrice(equipmentJson.getInteger("total_price"));
                e.setSalePrice(equipmentJson.getInteger("price"));
                equipmentMap.put(id,e);
            }
        }
    }

    private void initHeroEquipmentData(){
        String classPath=this.getClass().getClassLoader().getResource("").getPath();
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(classPath+"heroequipment.json");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        JSONReader jsonReader = new JSONReader(fileReader);
        String heroString=jsonReader.readString();
        //读取json文件，转为JSONArray
        JSONArray jsonArray = JSON.parseArray(heroString);
        if(jsonArray.size()>0){
            for(int i=0;i<jsonArray.size();i++){
                JSONObject heroequipment = jsonArray.getJSONObject(i);
                Integer heroId=heroequipment.getInteger("heroId");
                JSONArray heroequipmentJSONArray=heroequipment.getJSONArray("equipments");
                Integer[] equpids=new Integer[heroequipmentJSONArray.size()];
                for(int j=0;j<heroequipmentJSONArray.size();j++){
                    equpids[j]=heroequipmentJSONArray.getInteger(j);
                }
                if(heroEquipmentMap.containsKey(heroId)){
                    List<Integer[]> equipmentList=heroEquipmentMap.get(heroId);
                    equipmentList.add(equpids);
                    heroEquipmentMap.put(heroId,equipmentList);
                }else{
                    List<Integer[]> equipmentList= new ArrayList<>();
                    equipmentList.add(equpids);
                    heroEquipmentMap.put(heroId,equipmentList);
                }
            }
        }
    }

    public EquipmentServiceImpl(){
        this.initEquipmentData();
        this.initHeroEquipmentData();
    }

    @Override
    public HeroEquipment getHeroEquipment(Hero hero) {
        List<Integer[]> equpids=this.heroEquipmentMap.get(hero.getHeroId());
        Integer[] eids=equpids.get(0);
        if(equpids.size()>1){
            Random random=new Random();
            Integer pos=random.nextInt(equpids.size());
            eids=equpids.get(pos);
        }
        HeroEquipment heroEquipment=new HeroEquipment();
        heroEquipment.setPosition1(equipmentMap.get(eids[0]));
        heroEquipment.setPosition2(equipmentMap.get(eids[1]));
        heroEquipment.setPosition3(equipmentMap.get(eids[2]));
        heroEquipment.setPosition4(equipmentMap.get(eids[3]));
        heroEquipment.setPosition5(equipmentMap.get(eids[4]));
        heroEquipment.setPosition6(equipmentMap.get(eids[5]));
        return heroEquipment;
    }
}
